//服务器地址
var site = 'http://52.80.81.212:9333';
$(function() {
	//获取 rem
	var html = document.documentElement;
	var width = html.getBoundingClientRect().width;
	html.style.fontSize = width / 15 + 'px';
})
//首页banner轮播
function banner() {
	var mySwiper = new Swiper('.banner', {
		loop: true,
		autoplay: 4000,
		autoplayDisableOnInteraction: false,
		pagination: '.swiper-pagination',
		observer: true, //修改swiper自己或子元素时，自动初始化swiper
	});
}

function times(jsons) {
	Vue.filter('times',
		function() {
			var dateString = jsons.appointmentTime
			var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
			var formatedDate = dateString.replace(pattern, '$1-$2-$3 $4:$5');
			console.log(formatedDate)
			return formatedDate
		})
}

//弹窗方法
function alerts(s) {
	layer.open({
		content: s,
		skin: 'msg',
		time: 2 //2秒后自动关闭
	});
}

function btnremove() {
	//询问框
	layer.open({
		content: '确认删除？',
		btn: ['确认', '取消'],
		yes: function(index) {
			$.ajax({
				url: site + '/upkeep/delOrder',
				type: 'post',
				dataType: 'json',
				data: {
					userId: loca_fetch('userid'),
					orderId: loca_fetch('moderid')
				},
				success: function(data) {
					console.log(data)
					location.reload();
				}
			})
			layer.close(index);
		}
	});
}

//本地缓存
function loca_save(STORAGE_KEY, items) {
	window.localStorage.setItem(STORAGE_KEY, JSON.stringify(items))
}

function loca_fetch(STORAGE_KEY) {
	return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]')
}

//时间戳转换时间
function time() {
	Vue.filter('time',
		function(value) {
			var date = new Date(value);
			Y = date.getFullYear(),
				m = date.getMonth() + 1,
				d = date.getDate(),
				H = date.getHours(),
				i = date.getMinutes(),
				s = date.getSeconds();
			if(m < 10) {
				m = '0' + m;
			}
			if(d < 10) {
				d = '0' + d;
			}
			if(H < 10) {
				H = '0' + H;
			}
			if(i < 10) {
				i = '0' + i;
			}
			if(s < 10) {
				s = '0' + s;
			}
			var t = Y + '-' + m + '-' + d + ' ' + H + ':' + i;
			//var t = Y + '-' + m + '-' + d;
			return t;
		});
}

function getMyDate(str) {
	var oDate = new Date(str),
		oYear = oDate.getFullYear(),
		oMonth = oDate.getMonth() + 1,
		oDay = oDate.getDate(),
		oHour = oDate.getHours(),
		oMin = oDate.getMinutes(),
		oSen = oDate.getSeconds(),
		oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin); //最后拼接时间
	//oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay)//最后拼接时间  
	return oTime;
};

//补0操作
function getzf(num) {
	if(parseInt(num) < 10) {
		num = '0' + num;
	}
	return num;
}

//使用的消费金
function getIntegral(userId, totalMoney) {
	var rs;
	$.ajaxSetup({
		async: false
	});
	$.get(site + "/user/myIntegral", {
		"userId": userId,
		"rateType": 3
	}, function(da) {
		var rates = da.data.rates,
			experience = da.data.experience, //持有的消费金
			need = Math.ceil(totalMoney * rates); //需要的消费金(向上取整)
		if(experience == 0) {
			rs = 0;
		} else {
			if(experience <= need) {
				rs = experience;
			} else if(experience > need) {
				rs = need;
			}
		}
	});
	return rs;
}

//抵扣的金额
function getDMoney(userId, totalMoney) {
	var rs;
	$.ajaxSetup({
		async: false
	});
	$.get(site + "/user/myIntegral", {
		"userId": userId,
		"rateType": 3
	}, function(da) {
		var rates = da.data.rates,
			experience = da.data.experience,
			need = Math.ceil(totalMoney * rates); //需要的积分(向上取整)
		if(experience == 0) {
			rs = 0;
		} else {
			if(experience <= need) {
				rs = Math.floor(experience * 100 / rates) / 100;
			} else if(experience > need) {
				rs = Math.floor(need * 100 / rates) / 100;
			}
		}
	});
	return rs;
}

//计算贷款金额
function dTotalMoney(userId, totalMoney) {
	var rs;
	$.ajaxSetup({
		async: false
	});
	$.get(site + "/user/myIntegral", {
		"userId": userId,
		"rateType": 3
	}, function(da) {
		var rates = da.data.rates,
			experience = da.data.experience,
			need = Math.ceil(totalMoney * rates); //需要的积分(向上取整)
		if(experience == 0) {
			rs = 0;
		} else {
			if(experience <= need) {
				rs = Math.floor(experience * 100 / rates) / 100;
			} else if(experience > need) {
				rs = Math.floor(need * 100 / rates) / 100;
			}
		}
		if(totalMoney - rs <= 0) {
			rs = 0
		} else {
			rs = totalMoney - rs;
		}
	});
	return rs;
}

/*
 * 合同状态
 * */
function formatLoanStatus(val) {
	if(val == "FIRST_TRIAL_PASS") {
		return "初审通过";
	} else if(val == "WAIT_FIRST_CHECK") {
		return "待初审";
	} else if(val == "WAIT_CHECK") {
		return "待审核";
	} else if(val == "PASS_CHECK") {
		return "审核通过";
	} else if(val == "DISMISSAL") {
		return "已驳回";
	} else if(val == "RESCINDED") {
		return "已撤销";
	} else if(val == "REPAYMENTS") {
		return "还款中";
	} else if(val == "ALREADY_INSURED") {
		return "已投保";
	} else if(val == "COMPLETED") {
		return "已完成";
	} else if(val == "WAIT_SIGN") {
		return "待签名";
	} else {
		return val;
	}
}